﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Model;
using SDT.Model.Properties;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Lib.Helpers;
using System.Web.UI.WebControls;
namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class ProjectCategoryController : Controller
    {
        /// Created by Vietlv
        /// Date 17/06/2011
        /// Description: Manage ProjectCategory - Class ProjectCategory controler;
        /// </summary>
        /// <returns></returns>

        ChoVietDataContext eDataContext = new ChoVietDataContext();

        #region GET: /ProjectCategory/

        public ActionResult Index(int? page)
        {
            try
            {
                    if (page == null)
                        page = 1;
                    string order = UserState.getUserStateFromRequest("ProjectCategory.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("ProjectCategory.orderDir", "orderDir", "desc").ToString();
                    string keyword = UserState.getUserStateFromRequest("ProjectCategory.Keyword", "Keyword", "").ToString();
                    int pageSize = int.Parse(UserState.getUserStateFromRequest("ProjectCategory.pageSize", "PageSize", 20).ToString());
                    string strPub = UserState.getUserStateFromRequest("ProjectCategory.CurrentPubStatus", "CurrentPubStatus", "").ToString();

                    int? CurrentPubStatus = null;
                    if (!string.IsNullOrEmpty(strPub))
                        CurrentPubStatus = Convert.ToInt32(strPub);

                    var projectCategories = eDataContext.ProjectCategories
                        .Where(c => CurrentPubStatus.HasValue ? c.ActiveYN == (CurrentPubStatus.HasValue && CurrentPubStatus.Value == 1 ? true : false) : true)
                        .Where(c => (string.IsNullOrEmpty(keyword) ? true : c.Name.ToLower().Contains(keyword.ToLower().Trim())
                        ))
                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                        .ToPagedList(page.Value - 1, pageSize);

                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;

                    return View(new ProjectCategoryViewModel
                    {
                        ProjectCategories = projectCategories,
                        CurrentPubStatus = CurrentPubStatus,
                        Keyword = keyword,
                        ImageRootUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /ProjectCategory/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                    var proCat = eDataContext.ProjectCategories.Where(c => c.ID == id).SingleOrDefault();
                    var parent = eDataContext.ProjectCategories.Where(c => c.ID == proCat.ParentID).SingleOrDefault();

                    return View(new ProjectCategoryDetailModel
                    {
                        ParentCategoory = parent,
                        ProjectCategory = proCat
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /ProjectCategory/Create

        public ActionResult Create()
        {
            try
            {
                    List<TreeNodeItem> listTree = eDataContext.ProjectCategories
                                                    .Select(c => new TreeNodeItem()
                                                    {
                                                        Value = c.ID,
                                                        Text = c.Name,
                                                        ParentID = c.ParentID
                                                    }).ToList();
                    TreeNodeCollection categories = new BuiltTreeCollection().BuiltTreeNodeCollection(listTree);

                    return View(new ProjectCategoryInputModels
                    {
                        Categories = categories
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region POST: /ProjectCategory/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                    // TODO: Add insert logic here
                    ProjectCategory proCat = new ProjectCategory();
                    UpdateModel(proCat);
                    proCat.AddBy = UserManager.GetUserId();
                    proCat.AddDate = DateTime.Now;
                    eDataContext.ProjectCategories.InsertOnSubmit(proCat);
                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /ProjectCategory/Delete/5

        public ActionResult Delete(int id)
        {
            var currentItem = eDataContext.ProjectCategories.Single(e => e.ID == id);
            return View(currentItem);
        }
        #endregion

        #region POST: /ProjectCategory/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var currentItem = eDataContext.ProjectCategories.Single(e => e.ID == id);
                eDataContext.ProjectCategories.DeleteOnSubmit(currentItem);
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /ProjectCategory/Edit/5

        public ActionResult Edit(int id)
        {
            try
            {
                    var proCat = eDataContext.ProjectCategories.Where(c => c.ID == id).SingleOrDefault();

                    List<TreeNodeItem> listTree = eDataContext.ProjectCategories
                                                    .Select(c => new TreeNodeItem()
                                                    {
                                                        Value = c.ID,
                                                        Text = c.Name,
                                                        ParentID = c.ParentID
                                                    }).ToList();
                    TreeNodeCollection categories = new BuiltTreeCollection().BuiltTreeNodeCollection(listTree, proCat.ParentID);

                    return View(new ProjectCategoryEditModels
                    {
                        ListParentCategories = categories,
                        ProjectCategory = proCat
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region POST: /ProjectCategory/Edit/5
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                    ProjectCategory proCat = eDataContext.ProjectCategories.Where(c => c.ID == id).SingleOrDefault();
                    UpdateModel(proCat);
                    proCat.ModifyBy = UserManager.GetUserId();
                    proCat.ModifyDate = DateTime.Now;
                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion
    }
}
